package com.lanou.tipsy.adapter.singer;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.lanou.tipsy.R;
import com.lanou.tipsy.entity.singer.singerList.SingerRank;
import com.lanou.tipsy.tools.volley.VolleySingleton;

import java.util.ArrayList;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by FengJun on 15/10/22.
 * 歌手适配器
 */
public class SingersAdapter extends BaseAdapter {
    private ArrayList<SingerRank> datas;
    private Context context;
    private LayoutInflater inflater;
    private ImageLoader imageLoader;

    public SingersAdapter(ArrayList<SingerRank> datas, Context context) {
        this.datas = datas;
        this.context = context;
        inflater = LayoutInflater.from(context);
        imageLoader = VolleySingleton.getInstance().getImageLoader();
    }

    @Override
    public int getCount() {
        return datas != null && datas.size() > 0 ? 20 : 0;
    }

    @Override
    public Object getItem(int position) {
        return datas != null && datas.size() > 0 ? datas.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_singers, null);
            viewHolder = new ViewHolder();
            viewHolder.imageView = (CircleImageView) convertView.findViewById(R.id.ci_top_picture);
            viewHolder.textView = (TextView) convertView.findViewById(R.id.tv_top_name);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        SingerRank rank = (SingerRank) getItem(position);
        String url = rank.getPic_url();
        ImageLoader.ImageListener listener = ImageLoader.getImageListener(viewHolder.imageView,
                R.mipmap.logo_2, R.mipmap.error);
        imageLoader.get(url, listener);
        viewHolder.textView.setText(rank.getSinger_name());
        return convertView;
    }

    private class ViewHolder {
        private CircleImageView imageView;
        private TextView textView;
    }
}
